package com.bawei.document.view

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.bawei.document.DocumentIntent
import com.bawei.document.DocumentViewModel
import com.bawei.document.entity.DocumentEntity
import com.bawei.document.entity.DocumentEntityItem
import com.bawei.lib_base.StateType
import com.bawei.lib_base.UIState
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Composable
fun DocumentPage(modifier: Modifier = Modifier,navHostController: NavHostController,vm:DocumentViewModel= hiltViewModel()) {

    var list = remember {
        SnapshotStateList<DocumentEntityItem>()
    }
    LaunchedEffect(key1 = "") {
        launch {
            vm.state.collect(){
                when(it){
                   is UIState.OnError->{
                        Log.d("TAG", "获取文件列表失败: ${it.msg}")
                    }
                    UIState.OnLoading->{}
                    is UIState.OnSuccess<*>->{
                        if (it.type==StateType.DEFAULT){
                            list.clear()
                            list.addAll(it.data as DocumentEntity)
                        }
                    }
                }
            }

        }
        vm.sendUIIntent(DocumentIntent.GetDocument)
    }
    Column (modifier = Modifier.padding(10.dp)){
        Box {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "",Modifier.clickable {
                navHostController.popBackStack()
            })
            Text(text = "文件列表", textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth())
        }
    }

}